
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>5.2 secret 和 ConfigMap 卷 · Kubernetes - 痴者工良</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="痴者工良">
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.nfts.html" />
    
    
    <link rel="prev" href="1.volumes.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://www.whuanle.cn" target="_blank" class="custom-link">痴者工良的博客</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    文档说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../1.basic/">
            
                <a href="../1.basic/">
            
                    
                    1.基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../1.basic/">
            
                <a href="../1.basic/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../1.basic/1.docker.html">
            
                <a href="../1.basic/1.docker.html">
            
                    
                    1.1 说透 Docker：基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../1.basic/2.virtual.html">
            
                <a href="../1.basic/2.virtual.html">
            
                    
                    1.2 说透 Docker： 虚拟化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../1.basic/3.docker_network.html">
            
                <a href="../1.basic/3.docker_network.html">
            
                    
                    1.3 了解 Docker 网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../1.basic/4.pod_docker.html">
            
                <a href="../1.basic/4.pod_docker.html">
            
                    
                    1.4 Docker 和 Pod
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../1.basic/5.k8s.html">
            
                <a href="../1.basic/5.k8s.html">
            
                    
                    1.5 K8S入门基础
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../2.deploy/">
            
                <a href="../2.deploy/">
            
                    
                    2.部署和配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../2.deploy/2.deploy.md">
            
                <span>
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../2.deploy/1.minikube.html">
            
                <a href="../2.deploy/1.minikube.html">
            
                    
                    2.1 使用 Minikube 部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../2.deploy/2.kubeadm.html">
            
                <a href="../2.deploy/2.kubeadm.html">
            
                    
                    2.2 使用 kubeadm 部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../2.deploy/3.kubeadm_ckad.html">
            
                <a href="../2.deploy/3.kubeadm_ckad.html">
            
                    
                    2.3 CKAD认证中的部署教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../2.deploy/4.kubeadm_proxy.html">
            
                <a href="../2.deploy/4.kubeadm_proxy.html">
            
                    
                    2.4 国内代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../2.deploy/5.dashboard.html">
            
                <a href="../2.deploy/5.dashboard.html">
            
                    
                    2.5 Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../3.pod/">
            
                <a href="../3.pod/">
            
                    
                    3.Pod部署和调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../3.pod/">
            
                <a href="../3.pod/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../3.pod/1.pod.html">
            
                <a href="../3.pod/1.pod.html">
            
                    
                    3.1 Pod
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../3.pod/2.deployment.html">
            
                <a href="../3.pod/2.deployment.html">
            
                    
                    3.2 Deployment部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../3.pod/3.replica.html">
            
                <a href="../3.pod/3.replica.html">
            
                    
                    3.3 副本集(ReplicaSet)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../3.pod/4.pod_network.html">
            
                <a href="../3.pod/4.pod_network.html">
            
                    
                    3.34 Pod 端口映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../3.pod/5.update.html">
            
                <a href="../3.pod/5.update.html">
            
                    
                    3.5 Pod 升级、回滚
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../3.pod/6.scale.html">
            
                <a href="../3.pod/6.scale.html">
            
                    
                    3.6 Pod 缩放
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../3.pod/7.lable.html">
            
                <a href="../3.pod/7.lable.html">
            
                    
                    3.7.Pod 标签
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../3.pod/8.schedule.html">
            
                <a href="../3.pod/8.schedule.html">
            
                    
                    3.8 Pod 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../3.pod/9.jobs_cronjobs.html">
            
                <a href="../3.pod/9.jobs_cronjobs.html">
            
                    
                    3.9 Jobs、CronJobs
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../4.network/">
            
                <a href="../4.network/">
            
                    
                    4.Kubernetes 网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../4.network/">
            
                <a href="../4.network/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../4.network/1.network.html">
            
                <a href="../4.network/1.network.html">
            
                    
                    4.1 Kubernetes 网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../4.network/2.endpoint.html">
            
                <a href="../4.network/2.endpoint.html">
            
                    
                    4.2 Endpoint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../4.network/3.ingress.html">
            
                <a href="../4.network/3.ingress.html">
            
                    
                    4.3 ingress
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../4.network/4.discovery.html">
            
                <a href="../4.network/4.discovery.html">
            
                    
                    4.4 服务发现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="./">
            
                <a href="./">
            
                    
                    5.volumes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="./">
            
                <a href="./">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="1.volumes.html">
            
                <a href="1.volumes.html">
            
                    
                    5.1 卷
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.3" data-path="2.secret_configmap.html">
            
                <a href="2.secret_configmap.html">
            
                    
                    5.2 secret 和 ConfigMap 卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="3.nfts.html">
            
                <a href="3.nfts.html">
            
                    
                    5.3 NFS卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="4.pv_pvc.html">
            
                <a href="4.pv_pvc.html">
            
                    
                    5.4 持久化卷
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../6.api_safe/">
            
                <a href="../6.api_safe/">
            
                    
                    6.API 和 安全
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../6.api_safe/">
            
                <a href="../6.api_safe/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../7.health/">
            
                <a href="../7.health/">
            
                    
                    7.维护和故障排除
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../8.health/README.md">
            
                <span>
            
                    
                    导读
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../k8s.html">
            
                <a href="../k8s.html">
            
                    
                    Kubernetes 命令概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../ckad.html">
            
                <a href="../ckad.html">
            
                    
                    CKAD 认证帮助
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >5.2 secret 和 ConfigMap 卷</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#52-configmap&#x548C;-secret"><b></b>5.2 ConfigMap&#x548C; Secret</a></li><ul><li><span class="title-icon "></span><a href="#configmap"><b></b>ConfigMap</a></li><ul><li><span class="title-icon "></span><a href="#&#x521B;&#x5EFA;-configmap"><b></b>&#x521B;&#x5EFA; configMap</a></li><li><span class="title-icon "></span><a href="#&#x5728;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x4F7F;&#x7528;"><b></b>&#x5728;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x4F7F;&#x7528;</a></li><li><span class="title-icon "></span><a href="#configmap-&#x5377;"><b></b>ConfigMap &#x5377;</a></li><li><span class="title-icon "></span><a href="#&#x591A;&#x952E;&#x503C;&#x5BF9;"><b></b>&#x591A;&#x952E;&#x503C;&#x5BF9;</a></li></ul><li><span class="title-icon "></span><a href="#secret"><b></b>Secret</a></li><ul><li><span class="title-icon "></span><a href="#tls"><b></b>TLS</a></li><li><span class="title-icon "></span><a href="#&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret"><b></b>&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1; Secret</a></li><li><span class="title-icon "></span><a href="#ssh-&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret"><b></b>SSH &#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1; Secret</a></li><li><span class="title-icon "></span><a href="#&#x4F7F;&#x7528;-secret"><b></b>&#x4F7F;&#x7528; Secret</a></li><li><span class="title-icon "></span><a href="#&#x540C;&#x65F6;&#x4F7F;&#x7528;-configmap&#x3001;secret"><b></b>&#x540C;&#x65F6;&#x4F7F;&#x7528; ConfigMap&#x3001;Secret</a></li></ul></ul></ul></div><h1 id="52-configmap&#x548C;-secret"><a name="52-configmap&#x548C;-secret" class="anchor-navigation-ex-anchor" href="#52-configmap&#x548C;-secret"><i class="fa fa-link" aria-hidden="true"></i></a>5.2 ConfigMap&#x548C; Secret</h1>
<p>ConfigMap &#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5B58;&#x50A8;&#x975E;&#x673A;&#x5BC6;&#x6027;&#x7684;&#x6570;&#x636E;&#x5230;&#x952E;&#x503C;&#x5BF9;&#x4E2D;&#xFF0C;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x4F1A;&#x88AB;&#x5B58;&#x50A8;&#x5230; etcd&#xFF0C;&#x4E0D;&#x4F1A;&#x5206;&#x8282;&#x70B9;&#xFF0C;&#x4EFB;&#x4F55;&#x8282;&#x70B9;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5230;&#x3002;&#x4F7F;&#x7528;&#x65F6;&#xFF0C;&#x5C06;&#x5176;&#x7528;&#x4F5C;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3001;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#x6216;&#x8005;&#x5B58;&#x50A8;&#x5377;&#x4E2D;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x9001;&#x5165;&#x5230; Pod &#x4E2D;&#xFF0C;&#x4E3B;&#x8981;&#x76EE;&#x7684;&#x662F;&#x89E3;&#x8026;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x548C;&#x914D;&#x7F6E;&#xFF0C;&#x8FD9;&#x6837;&#x4E0D;&#x5FC5;&#x7EF4;&#x62A4;&#x90A3;&#x4E9B; .json &#x7B49;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x4E0D;&#x5C0F;&#x5FC3;&#x5C06;&#x5E26;&#x6709;&#x673A;&#x5BC6;&#x4FE1;&#x606F;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E0A;&#x4F20;&#x5230;&#x4EE3;&#x7801;&#x4ED3;&#x5E93;&#x4E2D;&#x3002;&#x4F46;&#x662F; ConfigMap  &#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x90FD;&#x662F;&#x975E;&#x52A0;&#x5BC6;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x5F88;&#x5BB9;&#x6613;&#x5730;&#x770B;&#x5230;&#x5168;&#x90E8;&#x4FE1;&#x606F;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x52A0;&#x5BC6;&#xFF0C;&#x5219;&#x4F7F;&#x7528; secret&#x3002;</p>
<h2 id="configmap"><a name="configmap" class="anchor-navigation-ex-anchor" href="#configmap"><i class="fa fa-link" aria-hidden="true"></i></a>ConfigMap</h2>
<h3 id="&#x521B;&#x5EFA;-configmap"><a name="&#x521B;&#x5EFA;-configmap" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;-configmap"><i class="fa fa-link" aria-hidden="true"></i></a>&#x521B;&#x5EFA; configMap</h3>
<p>&#x521B;&#x5EFA; configMap &#x7684;&#x547D;&#x4EE4;&#x683C;&#x5F0F;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create configmap <span class="token operator">&lt;</span>map-name<span class="token operator">&gt;</span> <span class="token operator">&lt;</span>data-source<span class="token operator">&gt;</span>
</code></pre>
<pre class="language-"><code class="lang-bash">kubectl create configmap NAME <span class="token punctuation">[</span>--from-file<span class="token operator">=</span><span class="token punctuation">[</span>key<span class="token operator">=</span><span class="token punctuation">]</span>source<span class="token punctuation">]</span> <span class="token punctuation">[</span>--from-literal<span class="token operator">=</span>key1<span class="token operator">=</span>value1<span class="token punctuation">]</span> <span class="token punctuation">[</span>--dry-run<span class="token operator">=</span>server<span class="token operator">|</span>client<span class="token operator">|</span>none<span class="token punctuation">]</span>
</code></pre>
<blockquote>
<p>&#x5982;&#x679C;&#x5FD8;&#x8BB0;&#x4E86;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>kubect create configmap --help</code> &#x5FEB;&#x901F;&#x83B7;&#x53D6;&#x6587;&#x6863;&#x8BF4;&#x660E;&#x3002;</p>
</blockquote>
<p>&#x5982;&#x679C;&#x4EE5;&#x6587;&#x4EF6;(<code>--from-file</code>)&#x5F62;&#x5F0F;&#x521B;&#x5EFA; ConfigMap&#xFF0C;&#x5219;&#x4E3A; key&#xFF0C;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x4E3A; value&#xFF0C;<code>--from-file</code> &#x4E5F;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x76EE;&#x5F55;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4EE5;&#x952E;&#x503C;&#x5BF9;(<code>--from-literal</code>)&#x5F62;&#x5F0F;&#x521B;&#x5EFA; ConfigMap&#xFF0C;&#x53EF;&#x76F4;&#x63A5;&#x521B;&#x5EFA; <code>key=value</code>&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x3001;&#x76EE;&#x5F55;&#x6216;&#x952E;&#x503C;&#x5BF9;&#x521B;&#x5EFA; ConfigMap&#xFF0C;ConfigMap &#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x591A;&#x4E2A;&#x6765;&#x6E90;&#x3002;&#x5F53;&#x57FA;&#x4E8E;&#x76EE;&#x5F55;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; ConfigMap&#x65F6;&#xFF0C;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x57FA;&#x540D;&#x662F;&#x8BE5;&#x76EE;&#x5F55;&#x4E2D;&#x7684;&#x6709;&#x6548;&#x5BC6;&#x94A5;&#xFF0C;&#x8FD9;&#x4E9B;&#x6587;&#x4EF6;&#x5C06;&#x88AB;&#x6253;&#x5305;&#x5230; ConfigMap&#x4E2D;&#x3002;</p>
<blockquote>
<p>&#x8FD9;&#x4E09;&#x70B9;&#x90FD;&#x662F;&#x4ECE;&#x54EA;&#x91CC;&#x751F;&#x6210; k-v &#x5230; ConfigMap&#xFF0C;&#x4E0D;&#x8981;&#x7EA0;&#x7ED3;&#x4E8E;&#x5B57;&#x9762;&#x3002;</p>
</blockquote>
<p>&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x952E;&#x503C;&#x5BF9;&#x521B;&#x5EFA; ConfigMap&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create configmap my-config --from-literal<span class="token operator">=</span>key1<span class="token operator">=</span>config1 --from-literal<span class="token operator">=</span>key2<span class="token operator">=</span>config2
</code></pre>
<p>&#x67E5;&#x770B; ConfigMap&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl get configmap my-config</span>
Data
<span class="token operator">==</span><span class="token operator">==</span>
key1:
----
config1
key2:
----
config2
</code></pre>
<blockquote>
<p><code>--from-literal</code> &#x8868;&#x793A;&#xFF0C;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x952E;&#x503C;&#x5BF9;&#x8D4B;&#x503C;&#x7684;&#x5F62;&#x5F0F;&#x521B;&#x5EFA; ConfigMap&#x3002;</p>
</blockquote>
<p>&#x901A;&#x8FC7;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x751F;&#x6210; ConfigMap&#xFF0C;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash"><span class="token assign-left variable">enemies</span><span class="token operator">=</span>aliens
<span class="token assign-left variable">lives</span><span class="token operator">=</span><span class="token number">3</span>
enemies.cheat<span class="token operator">=</span>true
enemies.cheat.level<span class="token operator">=</span>noGoodRotten
secret.code.passphrase<span class="token operator">=</span>UUDDLRLRBABAS
secret.code.allowed<span class="token operator">=</span>true
secret.code.lives<span class="token operator">=</span><span class="token number">30</span>
</code></pre>
<blockquote>
<p>&#x8BBE;&#x7F6E;&#x6587;&#x4EF6;&#x540D;&#x79F0;&#x4E3A; c.txt&#x3002;</p>
</blockquote>
<p>&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x4F7F;&#x7528;&#x952E;&#x503C;&#x5BF9;&#x5F62;&#x5F0F;&#x3002;</p>
<p>&#x521B;&#x5EFA; ConfigMap&#xFF1A;</p>
<pre class="language-"><code>kubectl create configmap my-config1 --from-file=c.txt
</code></pre><p>&#x67E5;&#x770B; ConfigMap&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl describe configmap my-config1</span>
Data
<span class="token operator">==</span><span class="token operator">==</span>
c.txt:
----
<span class="token assign-left variable">enemies</span><span class="token operator">=</span>aliens
<span class="token assign-left variable">lives</span><span class="token operator">=</span><span class="token number">3</span>
enemies.cheat<span class="token operator">=</span>true
enemies.cheat.level<span class="token operator">=</span>noGoodRotten
secret.code.passphrase<span class="token operator">=</span>UUDDLRLRBABAS
secret.code.allowed<span class="token operator">=</span>true
secret.code.lives<span class="token operator">=</span><span class="token number">30</span>
</code></pre>
<blockquote>
<p>&#x591A;&#x4E2A;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code>kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
</code></pre></blockquote>
<p>&#x901A;&#x8FC7;&#x76EE;&#x5F55;&#x521B;&#x5EFA; ConfigMap&#xFF0C;&#x521B;&#x5EFA;&#x76EE;&#x5F55;&#x548C;&#x6587;&#x4EF6;&#xFF0C;&#x5176;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">&#x251C;&#x2500;&#x2500; config
&#x2502;   &#x251C;&#x2500;&#x2500; a.txt
&#x2502;   &#x2514;&#x2500;&#x2500; b.txt
</code></pre>
<blockquote>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># cat config/a.txt </span>
<span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token number">666</span>
root@master:~<span class="token comment"># cat config/b.txt </span>
<span class="token assign-left variable">b</span><span class="token operator">=</span><span class="token number">666</span>
</code></pre>
</blockquote>
<p>&#x521B;&#x5EFA; ConfigMap&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create configmap my-config2 --from-file<span class="token operator">=</span>config/
</code></pre>
<p>&#x67E5;&#x770B; ConfigMap&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl describe configmap my-config2</span>
Data
<span class="token operator">==</span><span class="token operator">==</span>
a.txt:
----
<span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token number">666</span>

b.txt:
----
<span class="token assign-left variable">b</span><span class="token operator">=</span><span class="token number">666</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x901A;&#x8FC7; &#x6587;&#x4EF6; &#x6216;&#x76EE;&#x5F55;&#x521B;&#x5EFA;&#x7684; ConfigMap &#x4E2D;&#xFF0C;&#x6587;&#x4EF6;&#x540D;&#x79F0;&#x4E3A; Key&#xFF0C;&#x800C;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x4E3A; Value&#xFF0C;&#x6240;&#x4EE5; <code>a=666</code> &#x662F;&#x6587;&#x4EF6;&#x7684;&#x503C;&#xFF0C;&#x91CC;&#x9762;&#x4E0D;&#x80FD;&#x518D;&#x5206;&#x4E3A; Key a&#x3001;Value B&#xFF0C;<code>a=666</code> &#x662F;&#x4E00;&#x4E2A;&#x503C;&#x3002;</p>
<p>&#x6216;&#x8005;&#x6539;&#x6210;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x4F1A;&#x66F4;&#x5BB9;&#x6613;&#x7406;&#x89E3;&#xFF1A;</p>
<pre class="language-"><code>====
name.txt:
----
&#x75F4;&#x8005;&#x5DE5;&#x826F;

url.txt:
----
k8s.whuanle.cn
</code></pre></blockquote>
<p>&#x4E0B;&#x9762;&#x5C06;&#x4ECB;&#x7ECD;&#x600E;&#x4E48;&#x4F7F;&#x7528; ConfigMap&#x3002;</p>
<h3 id="&#x5728;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x4F7F;&#x7528;"><a name="&#x5728;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x5728;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5728;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x4F7F;&#x7528;</h3>
<p>&#x524D;&#x9762;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&#x4E86; my-config&#x3001; my-config1&#x3001; my-config2 &#x4E09;&#x4E2A;&#x914D;&#x7F6E;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x4E9B;&#x8FD9;&#x914D;&#x7F6E;&#xFF0C;&#x80FD;&#x591F;&#x4EE5;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x5F62;&#x5F0F;&#x4F20;&#x9012;&#x5230; &#x5BB9;&#x5668;&#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x5B9A;&#x4E49; Pod &#x65F6;&#x5176; YAML &#x6A21;&#x677F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">env</span><span class="token punctuation">:</span>
 <span class="token punctuation">-</span> name<span class="token punctuation">:</span>letter
   <span class="token key atrule">valueFrom</span><span class="token punctuation">:</span>
     <span class="token key atrule">configMapKeyRef</span><span class="token punctuation">:</span>
       <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>config2
       <span class="token key atrule">key</span><span class="token punctuation">:</span> a.txt
</code></pre>
<p>&#x6CE8;&#xFF1A;key &#x4E3A; <code>|</code> &#x4E4B;&#x524D;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x4F8B;&#x5982; <code>a.txt</code>&#xFF0C;&#x6587;&#x4EF6;&#x540D;&#xFF1B;</p>
<p>&#x6216;&#x8005;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml">      <span class="token key atrule">envFrom</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">configMapRef</span><span class="token punctuation">:</span>
          <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>config2
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x6574;&#x4E2A; configMap &#x7684;&#x5185;&#x5BB9;&#x5168;&#x90E8;&#x5BFC;&#x5165;&#xFF0C;&#x5219;&#x4F7F;&#x7528; envFrom&#xFF0C;&#x5982;&#x679C;&#x8981;&#x53EA;&#x4F7F;&#x7528;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x53EF;&#x4EE5;&#x7528; valueFrom&#x3002;</p>
<p>&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x6765;&#x771F;&#x5B9E;&#x542F;&#x52A8;&#x4E00;&#x4E2A; Nginx Pod&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span> 
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
<span class="token key atrule">spec</span><span class="token punctuation">:</span> 
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>latest
    <span class="token key atrule">env</span><span class="token punctuation">:</span> 
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myconfig_a
      <span class="token key atrule">valueFrom</span><span class="token punctuation">:</span> 
        <span class="token key atrule">configMapKeyRef</span><span class="token punctuation">:</span> 
          <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>config2
          <span class="token key atrule">key</span><span class="token punctuation">:</span> a.txt
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myconfig_b
      <span class="token key atrule">valueFrom</span><span class="token punctuation">:</span>
        <span class="token key atrule">configMapKeyRef</span><span class="token punctuation">:</span> 
          <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>config2
          <span class="token key atrule">key</span><span class="token punctuation">:</span>  b.txt
</code></pre>
<blockquote>
<p><strong>[Info] &#x63D0;&#x793A;</strong></p>
<p>&#x5982;&#x679C; ConfigMap &#x901A;&#x8FC7;&#x6587;&#x4EF6;&#x6216;&#x76EE;&#x5F55;&#x521B;&#x5EFA;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x662F;&#x6253;&#x5305;&#x4E00;&#x8D77;&#x7684;&#x3002;&#x6240;&#x4EE5;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml">        <span class="token key atrule">configMapKeyRef</span><span class="token punctuation">:</span> 
          <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>config2
          <span class="token key atrule">key</span><span class="token punctuation">:</span> a.txt
</code></pre>
<p>a.txt &#x4E2D;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x6620;&#x5C04;&#x5230; myconfig_a&#x3002;</p>
</blockquote>
<pre class="language-"><code class="lang-bash">kubectl apply -f configMapNginx.yaml
</code></pre>
<p>&#x6253;&#x5370; Pod &#x4E2D;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl exec nginx -- printenv</span>
<span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
<span class="token assign-left variable"><span class="token environment constant">HOSTNAME</span></span><span class="token operator">=</span>nginx
<span class="token assign-left variable">myconfig_a</span><span class="token operator">=</span>a<span class="token operator">=</span><span class="token number">666</span>

<span class="token assign-left variable">myconfig_b</span><span class="token operator">=</span>b<span class="token operator">=</span><span class="token number">666</span>
</code></pre>
<blockquote>
<p>&#x4E0D;&#x8FC7;&#x8FD9;&#x6837;&#x6620;&#x5C04;&#x4F1A;&#x6709;&#x7684;&#x5947;&#x602A;&#xFF0C;&#x56E0;&#x6B64;&#x4F7F;&#x7528;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4F7F;&#x7528; ConfigMap &#x65F6;&#xFF0C;&#x90FD;&#x662F;&#x4F7F;&#x7528; K/V &#x7684;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x4E0D;&#x4F7F;&#x7528;&#x901A;&#x8FC7;&#x6587;&#x4EF6;&#x3001;&#x76EE;&#x5F55;&#x7684;&#x6570;&#x636E;&#x3002;</p>
</blockquote>
<h3 id="configmap-&#x5377;"><a name="configmap-&#x5377;" class="anchor-navigation-ex-anchor" href="#configmap-&#x5377;"><i class="fa fa-link" aria-hidden="true"></i></a>ConfigMap &#x5377;</h3>
<p><code>configMap</code> &#x5377; &#x63D0;&#x4F9B;&#x4E86;&#x5411; Pod &#x6CE8;&#x5165;&#x914D;&#x7F6E;&#x6570;&#x636E;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x5176;&#x4E3B;&#x8981;&#x7528;&#x9014;&#x662F;&#x7ED9; Pod &#x4E2D;&#x7684;&#x5BB9;&#x5668;&#x4F20;&#x9012;&#x914D;&#x7F6E;&#xFF0C;&#x5728; Pod &#x4E2D;&#x663E;&#x793A;&#x4E3A;&#x6587;&#x4EF6;&#x3002;</p>
<p>Pod &#x7684; YAML &#x6587;&#x4EF6;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> configmap<span class="token punctuation">-</span>pod
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> configmap<span class="token punctuation">-</span>pod
      <span class="token key atrule">image</span><span class="token punctuation">:</span> busybox
      <span class="token key atrule">command</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">&quot;ls&quot;</span><span class="token punctuation">]</span>
      <span class="token key atrule">args</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">&quot;/etc/config&quot;</span><span class="token punctuation">]</span>
      <span class="token key atrule">volumeMounts</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> config<span class="token punctuation">-</span>vol
          <span class="token key atrule">mountPath</span><span class="token punctuation">:</span> /etc/config
  <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> config<span class="token punctuation">-</span>vol
      <span class="token key atrule">configMap</span><span class="token punctuation">:</span>
        <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>config
</code></pre>
<p>&#x67E5;&#x770B;&#x6B64; Pod &#x7684;&#x65E5;&#x5FD7;&#xFF1A;</p>
<pre class="language-"><code>root@master:~# kubectl logs  configmap-pod
key1
key2
</code></pre><blockquote>
<p>key1 &#x6587;&#x4EF6;&#x4E2D;&#x5B58;&#x50A8;&#x4E86; &#x503C;&#xFF0C;ConfigMap my-config &#x4E2D;&#x6709; <code>key1=config1</code>&#x3001;<code>key2=config2</code> &#x4E24;&#x4E2A;&#x914D;&#x7F6E;&#xFF0C;&#x5219;&#x5728; key1&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x5176;&#x503C;&#x4E3A; config1&#x3002;</p>
</blockquote>
<p>&#x4F46;&#x662F;&#x8FD9;&#x6837;&#x597D;&#x50CF;&#x6CA1;&#x5565;&#x610F;&#x601D;&#xFF0C;&#x65E2;&#x7136;&#x8981;&#x6620;&#x5C04;&#x4E3A;&#x6587;&#x4EF6;&#xFF0C;&#x5E94;&#x8BE5;&#x521B;&#x5EFA;&#x6709;&#x610F;&#x601D;&#x7684;&#x5185;&#x5BB9;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; config.json &#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-json"><span class="token punctuation">{</span>
    <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&#x75F4;&#x8005;&#x5DE5;&#x826F;&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;url&quot;</span><span class="token operator">:</span> <span class="token string">&quot;k8s.whuanle.cn&quot;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x521B;&#x5EFA; ConfigMap&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create configmap my-config3 --from-file<span class="token operator">=</span>config.json
</code></pre>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl describe configmap my-config3</span>
Name:         my-config3

config.json:
----
<span class="token punctuation">{</span>
  <span class="token string">&quot;name&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;&#x75F4;&#x8005;&#x5DE5;&#x826F;&quot;</span>,
  <span class="token string">&quot;url&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;k8s.whuanle.cn&quot;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6620;&#x5C04;&#x4E3A; Pod &#x4E2D;&#x7684;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">apiVersion: v1
kind: Pod
metadata:
  name: configmap-pod
spec:
  containers:
    - name: configmap-pod
      image: busybox
      command: <span class="token punctuation">[</span><span class="token string">&quot;cat&quot;</span><span class="token punctuation">]</span>
      args: <span class="token punctuation">[</span><span class="token string">&quot;/etc/config/config.json&quot;</span><span class="token punctuation">]</span>
      volumeMounts:
        - name: config-vol
          mountPath: /etc/config
  volumes:
    - name: config-vol
      configMap:
        name: my-config3
</code></pre>
<p>&#x67E5;&#x770B;&#x5BB9;&#x5668;&#x4E2D;&#x88AB;&#x6620;&#x5C04;&#x7684;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl logs configmap-pod</span>
<span class="token punctuation">{</span>
    <span class="token string">&quot;name&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;&#x75F4;&#x8005;&#x5DE5;&#x826F;&quot;</span>,
    <span class="token string">&quot;url&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;k8s.whuanle.cn&quot;</span>
<span class="token punctuation">}</span>
<span class="token comment"># cat /etc/config/config.json</span>
</code></pre>
<blockquote>
<p><strong>[Success] &#x63D0;&#x793A;</strong></p>
<p>&#x901A;&#x8FC7; ConfigMap &#x5377;&#x6620;&#x5C04;&#x5230;&#x5BB9;&#x5668;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x7684;&#x662F; mountPath &#x6302;&#x8F7D;&#x5230;&#x76EE;&#x5F55;&#xFF0C;&#x5219; ConfigMap &#x66F4;&#x65B0;&#x5185;&#x5BB9;&#x65F6;&#xFF0C;&#x5BB9;&#x5668;&#x4E2D;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x4E5F;&#x4F1A;&#x88AB;&#x66F4;&#x65B0;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x51E0;&#x5206;&#x949F;&#x3002;&#x5982;&#x679C;&#x6302;&#x8F7D;&#x7684;&#x76EE;&#x5F55;&#x662F;&#x5BB9;&#x5668;&#x4E2D;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#xFF0C;&#x5219;&#x53EF;&#x80FD;&#x65E0;&#x6CD5;&#x540C;&#x6B65;&#x66F4;&#x65B0;&#xFF0C;&#x56E0;&#x6B64;&#x6302;&#x8F7D; ConfigMap &#x5230;&#x5BB9;&#x5668;&#x4E2D;&#x7684;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x76EE;&#x5F55;&#x3002;</p>
</blockquote>
<h3 id="&#x591A;&#x952E;&#x503C;&#x5BF9;"><a name="&#x591A;&#x952E;&#x503C;&#x5BF9;" class="anchor-navigation-ex-anchor" href="#&#x591A;&#x952E;&#x503C;&#x5BF9;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x591A;&#x952E;&#x503C;&#x5BF9;</h3>
<p>&#x524D;&#x9762;&#x63D0;&#x5230;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x4E2A; a.txt &#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code>key1=value1
key2=value2
... ...
</code></pre><p>&#x5982;&#x679C;&#x4F7F;&#x7528; <code>--from-file</code> &#x7B49;&#x5BFC;&#x5165; ConfigMap &#x4E2D;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x8FBE;&#x5230;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x540C;&#x65F6;&#x5BFC;&#x5165;<strong>&#x591A;&#x4E2A;&#x503C;</strong>&#x7684;&#x6548;&#x679C;&#xFF0C;&#x5BFC;&#x5165;&#x540E;&#x4F1A;&#x53D8;&#x6210; <code>a.txt=&quot;key1=value1... ...&quot;</code>&#x3002;&#x5B83;&#x66F4;&#x53EF;&#x80FD;&#x4F7F;&#x7528; JSON&#x3001;YAML &#x6587;&#x4EF6;&#x6765;&#x8868;&#x8FBE;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre class="language-"><code class="lang-json"><span class="token punctuation">{</span>
    <span class="token property">&quot;ConnectString&quot;</span><span class="token operator">:</span> <span class="token string">&quot;mysql=192.168.0.1;user=admin... ...&quot;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x540E;&#x4F7F;&#x7528; <code>kubectl create configmap test --from-file=sql.config</code> &#xFF0C;&#x7136;&#x540E;&#x5728; Pod &#x4E2D;&#x4F7F;&#x7528; <code>sql.config</code> &#x8FD9;&#x4E2A;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x6216;&#x5377;&#x6587;&#x4EF6;&#x4F7F;&#x7528; Json &#x914D;&#x7F6E;&#x3002;</p>
<p>&#x5982;&#x679C;&#x8981;&#x5C06; a.txt &#x4E2D;&#x7684;&#x591A;&#x884C;&#x952E;&#x503C;&#x5BF9;&#x62C6;&#x5206;&#x51FA;&#x6765;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>--from-env-file</code> &#x3002;</p>
<p>&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x6709;&#x4E00;&#x5B9A;&#x7684;&#x89C4;&#x5219;&#xFF1A;</p>
<ul>
<li>&#x4EE5; # &#x5F00;&#x5934;&#x7684;&#x884C;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#xFF1B;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7A7A;&#x884C;&#x5206;&#x9694;&#xFF0C;&#x7A7A;&#x884C;&#x4E5F;&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#xFF1B;</li>
<li>&#x5177;&#x6709;&#x610F;&#x4E49;&#x7684;&#x884C;&#x5FC5;&#x987B;&#x662F; <code>{&#x952E;}={&#x503C;}</code> &#x7684;&#x5F62;&#x5F0F;&#xFF1B;</li>
<li>&#x5F15;&#x53F7;&#x4E0D;&#x4F1A;&#x88AB;&#x7279;&#x6B8A;&#x5904;&#x7406;&#xFF0C;&#x5373;&#x4E0D;&#x4F1A;&#x88AB;&#x8F6C;&#x4E49;&#x6216;&#x4E0D;&#x9700;&#x8981;&#x4F60;&#x63D0;&#x524D;&#x8F6C;&#x4E49;&#x3002;</li>
</ul>
<p>&#x5173;&#x4E8E;&#x4E0A;&#x9762;&#x8FD9;&#x4E09;&#x70B9;&#xFF0C;&#x521B;&#x5EFA; a.txt &#x793A;&#x4F8B;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code>enemies=aliens
lives=3
allowed=&quot;true&quot;
</code></pre><p>&#x5BFC;&#x5165;&#x547D;&#x4EE4;&#x683C;&#x5F0F;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create configmap my-config3 --from-env-file<span class="token operator">=</span>a.txt
</code></pre>
<p>&#x751F;&#x6210;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token punctuation">...</span> <span class="token punctuation">...</span>
<span class="token key atrule">data</span><span class="token punctuation">:</span>
  <span class="token key atrule">allowed</span><span class="token punctuation">:</span> <span class="token string">&apos;&quot;true&quot;&apos;</span>
  <span class="token key atrule">enemies</span><span class="token punctuation">:</span> aliens
  <span class="token key atrule">lives</span><span class="token punctuation">:</span> <span class="token string">&quot;3&quot;</span>
<span class="token punctuation">...</span> <span class="token punctuation">...</span>
</code></pre>
<blockquote>
<p><strong>[Info] &#x63D0;&#x793A;</strong></p>
<p>&#x547D;&#x4EE4;&#x4E2D;&#x53EA;&#x80FD;&#x51FA;&#x73B0;&#x4E00;&#x4E2A; <code>--from-env-file</code>&#xFF0C;&#x591A;&#x4E2A; <code>--from-env-file</code> &#x540C;&#x65F6;&#x4F7F;&#x7528;&#xFF0C;&#x53EA;&#x6709;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x3002;</p>
</blockquote>
<h2 id="secret"><a name="secret" class="anchor-navigation-ex-anchor" href="#secret"><i class="fa fa-link" aria-hidden="true"></i></a>Secret</h2>
<p>Secret &#x5377;&#x7528;&#x6765;&#x7ED9; Pod &#x4F20;&#x9012;&#x654F;&#x611F;&#x4FE1;&#x606F;&#xFF0C;&#x4F8B;&#x5982;&#x5BC6;&#x7801;&#x3001;&#x5BC6;&#x94A5;&#x7B49;&#x3002;Secret&#x5377;&#x5B9E;&#x9645;&#x4E0A;&#x4E0D;&#x662F;&#x7528;&#x4E8E;&#x5B58;&#x50A8;&#x7684;&#xFF0C;Secret &#x4E2D;&#x5B58;&#x50A8;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x4F1A;&#x4EE5;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3001;&#x6587;&#x4EF6;&#x7B49;&#x7684;&#x5F62;&#x5F0F;&#x663E;&#x793A;&#x5728; Pod &#x4E2D;&#x3002;&#x5728; 4.3 &#x7AE0;&#x7684; Ingress &#x4E2D;&#xFF0C;&#x5C31;&#x8BB2;&#x89E3;&#x8FC7;&#x4F7F;&#x7528; Secret &#x4E3A; Ingress &#x589E;&#x52A0; TLS &#x52A0;&#x5BC6;&#x8BBF;&#x95EE;(HTTPS)&#x3002;</p>
<p>Secret &#x7684;&#x4FE1;&#x606F;&#x5B58;&#x50A8;&#x5728; etcd &#x4E2D;&#xFF0C;&#x4F46;&#x662F; Secret &#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x4E5F;&#x4E0D;&#x662F;&#x52A0;&#x5BC6;&#x5B58;&#x50A8;&#x7684;&#xFF0C;&#x5173;&#x4E8E;&#x5982;&#x4F55;&#x52A0;&#x5BC6; Secret &#x540E;&#x9762;&#x7684;&#x7AE0;&#x8282;&#x4F1A;&#x4ECB;&#x7ECD;&#x5230;&#x3002;</p>
<blockquote>
<p><strong>&#x8BF4;&#x660E;&#xFF1A;</strong> &#x4F7F;&#x7528;&#x524D;&#x4F60;&#x5FC5;&#x987B;&#x5728; Kubernetes API &#x4E2D;&#x521B;&#x5EFA; Secret&#x3002;</p>
</blockquote>
<p>Secret &#x7684;&#x7C7B;&#x578B;&#x5F88;&#x591A;&#xFF0C;&#x8FD9;&#x91CC;&#x5C06;&#x5B98;&#x65B9;&#x6587;&#x6863;&#x4E2D;&#x5217;&#x4E3E;&#x7684; secret &#x7C7B;&#x578B;&#x590D;&#x5236;&#x4E00;&#x4EFD;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x5185;&#x7F6E;&#x7C7B;&#x578B;</th>
<th>&#x7528;&#x6CD5;</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Opaque</code></td>
<td>&#x7528;&#x6237;&#x5B9A;&#x4E49;&#x7684;&#x4EFB;&#x610F;&#x6570;&#x636E;</td>
</tr>
<tr>
<td><code>kubernetes.io/service-account-token</code></td>
<td>&#x670D;&#x52A1;&#x8D26;&#x53F7;&#x4EE4;&#x724C;</td>
</tr>
<tr>
<td><code>kubernetes.io/dockercfg</code></td>
<td><code>~/.dockercfg</code> &#x6587;&#x4EF6;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5F62;&#x5F0F;</td>
</tr>
<tr>
<td><code>kubernetes.io/dockerconfigjson</code></td>
<td><code>~/.docker/config.json</code> &#x6587;&#x4EF6;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5F62;&#x5F0F;</td>
</tr>
<tr>
<td><code>kubernetes.io/basic-auth</code></td>
<td>&#x7528;&#x4E8E;&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x7684;&#x51ED;&#x636E;</td>
</tr>
<tr>
<td><code>kubernetes.io/ssh-auth</code></td>
<td>&#x7528;&#x4E8E; SSH &#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x7684;&#x51ED;&#x636E;</td>
</tr>
<tr>
<td><code>kubernetes.io/tls</code></td>
<td>&#x7528;&#x4E8E; TLS &#x5BA2;&#x6237;&#x7AEF;&#x6216;&#x8005;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7684;&#x6570;&#x636E;</td>
</tr>
<tr>
<td><code>bootstrap.kubernetes.io/token</code></td>
<td>&#x542F;&#x52A8;&#x5F15;&#x5BFC;&#x4EE4;&#x724C;&#x6570;&#x636E;</td>
</tr>
</tbody>
</table>
<p>&#x8FD9;&#x91CC;&#x53EA;&#x8BB2;&#x89E3;&#x4E00;&#x90E8;&#x5206;&#x5E38;&#x7528;&#x7684; secret &#x7C7B;&#x578B;&#xFF0C;&#x5982;&#x9700;&#x4E86;&#x89E3;&#x66F4;&#x591A;&#x8BE6;&#x7EC6;&#x77E5;&#x8BC6;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;&#x5B98;&#x65B9;&#x6587;&#x6863;&#xFF1A;<a href="https://kubernetes.io/zh/docs/concepts/configuration/secret/" target="_blank">https://kubernetes.io/zh/docs/concepts/configuration/secret/</a></p>
<p>&#x4F7F;&#x7528; <code>kubectl</code> &#x547D;&#x4EE4;&#x521B;&#x5EFA; Secret &#x65F6;&#xFF0C;&#x547D;&#x4EE4;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create secret <span class="token punctuation">{</span>&#x7C7B;&#x578B;<span class="token punctuation">}</span> <span class="token punctuation">{</span>secret&#x540D;&#x79F0;<span class="token punctuation">}</span>
</code></pre>
<p>&#x5176;&#x4E2D;&#xFF0C;&#x666E;&#x901A;&#x7C7B;&#x578B;/Qpaque &#x7C7B;&#x578B;&#x7684;&#x7C7B;&#x578B;&#x540D;&#x79F0;&#x662F; <code>generic</code>&#xFF0C;&#x800C;&#x4E0D;&#x662F; <code>qpaque</code>&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>kubectl creatte secret generic</code> &#x547D;&#x4EE4;&#x521B;&#x5EFA; secret &#x65F6;&#xFF0C;&#x5BFC;&#x5165;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x683C;&#x5F0F;&#x8DDF; ConfigMap &#x4E00;&#x81F4;&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x518D;&#x591A;&#x8BF4;&#x4E86;&#xFF0C;&#x4E0B;&#x9762;&#x4ECB;&#x7ECD;&#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x5E38;&#x7528;&#x7684; Secret &#x7C7B;&#x578B;&#x3002;</p>
<h3 id="tls"><a name="tls" class="anchor-navigation-ex-anchor" href="#tls"><i class="fa fa-link" aria-hidden="true"></i></a>TLS</h3>
<p>&#x5728; 4.3 &#x7AE0;&#x7684; Ingrss &#x4E2D;&#xFF0C;&#x5DF2;&#x7ECF;&#x804A;&#x5230;&#x8FC7;&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x518D;&#x7EC6;&#x8C08;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x8BC1;&#x4E66;&#x521B;&#x5EFA; Secret &#x7684;&#x547D;&#x4EE4;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create secret tls tls-secret --cert<span class="token operator">=</span>1_k1.whuanle.cn_bundle.crt --key<span class="token operator">=</span>2_k1.whuanle.cn.key
</code></pre>
<p>&#x4F7F;&#x7528; YAML &#x8868;&#x793A;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">data</span><span class="token punctuation">:</span>
  <span class="token key atrule">tls.crt</span><span class="token punctuation">:</span> <span class="token punctuation">...</span>
  <span class="token key atrule">tls.key</span><span class="token punctuation">:</span> <span class="token punctuation">...</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Secret
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> tls<span class="token punctuation">-</span>secret
<span class="token key atrule">type</span><span class="token punctuation">:</span> kubernetes.io/tls
</code></pre>
<p>Nginx &#x914D;&#x7F6E; Https &#x8BC1;&#x4E66;&#xFF0C;&#x4F7F;&#x7528;&#x7684;&#x4FBF;&#x662F; crt&#x3001;key &#x6587;&#x4EF6;&#xFF0C;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#x7684;&#x8BC1;&#x4E66;&#x8BF7;&#x8BFB;&#x8005;&#x81EA;&#x884C;&#x67E5;&#x9605;&#x6587;&#x6863;&#x3002;</p>
<h3 id="&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret"><a name="&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret" class="anchor-navigation-ex-anchor" href="#&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret"><i class="fa fa-link" aria-hidden="true"></i></a>&#x57FA;&#x672C;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1; Secret</h3>
<p>&#x5176;&#x7C7B;&#x578B;&#x4E3A; <code>kubernetes.io/basic-auth</code>&#xFF0C;&#x5B83;&#x662F;&#x7528;&#x6765;&#x5B58;&#x50A8;&#x8D26;&#x53F7;&#x5BC6;&#x7801;&#x7684;&#xFF0C;<code>spec.data</code> &#x4E2D;&#x5FC5;&#x987B;&#x5305;&#x542B; username &#x548C; password &#x4E24;&#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x5176; YAML &#x6587;&#x4EF6;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Secret
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> secret<span class="token punctuation">-</span>basic<span class="token punctuation">-</span>auth
<span class="token key atrule">type</span><span class="token punctuation">:</span> kubernetes.io/basic<span class="token punctuation">-</span>auth
<span class="token key atrule">stringData</span><span class="token punctuation">:</span>
  <span class="token key atrule">username</span><span class="token punctuation">:</span> admin
  <span class="token key atrule">password</span><span class="token punctuation">:</span> t0p<span class="token punctuation">-</span>Secret
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x67E5;&#x770B;&#x6570;&#x636E;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml">root@master<span class="token punctuation">:</span><span class="token null important">~</span><span class="token comment"># kubectl get secret secret-basic-auth  -o yaml</span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">data</span><span class="token punctuation">:</span>
  <span class="token key atrule">password</span><span class="token punctuation">:</span> dDBwLVNlY3JldA==
  <span class="token key atrule">username</span><span class="token punctuation">:</span> YWRtaW4=
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Secret
<span class="token punctuation">...</span> <span class="token punctuation">...</span>
</code></pre>
<p>username &#x548C; password &#x7B49;&#x4F1A;&#x88AB;&#x4F7F;&#x7528; base64 &#x7F16;&#x7801;&#xFF0C;&#x4F46;&#x662F;&#x5E76;&#x4E0D;&#x662F;&#x52A0;&#x5BC6;&#x5C4F;&#x853D;&#xFF0C;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x53EF;&#x4EE5;&#x88AB;&#x76F4;&#x63A5;&#x7528; base64 &#x8FD8;&#x539F;&#x3002;</p>
<h3 id="ssh-&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret"><a name="ssh-&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret" class="anchor-navigation-ex-anchor" href="#ssh-&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;-secret"><i class="fa fa-link" aria-hidden="true"></i></a>SSH &#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1; Secret</h3>
<p>&#x5176;&#x7C7B;&#x578B;&#x662F;  <code>kubernetes.io/ssh-auth</code>&#xFF0C;&#x7528;&#x6765;&#x5B58;&#x50A8;&#x4E00;&#x6BB5; ssh &#x5BC6;&#x94A5;&#x3002;</p>
<p>&#x5176; YAML &#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Secret
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> secret<span class="token punctuation">-</span>ssh<span class="token punctuation">-</span>auth
<span class="token key atrule">type</span><span class="token punctuation">:</span> kubernetes.io/ssh<span class="token punctuation">-</span>auth
<span class="token key atrule">data</span><span class="token punctuation">:</span>
  <span class="token comment"># &#x6B64;&#x4F8B;&#x4E2D;&#x7684;&#x5B9E;&#x9645;&#x6570;&#x636E;&#x88AB;&#x622A;&#x65AD;</span>
  <span class="token key atrule">ssh-privatekey</span><span class="token punctuation">:</span> <span class="token punctuation">|</span><span class="token scalar string">
          MIIEpQIBAAKCAQEAulqb/Y ...</span>
</code></pre>
<h3 id="&#x4F7F;&#x7528;-secret"><a name="&#x4F7F;&#x7528;-secret" class="anchor-navigation-ex-anchor" href="#&#x4F7F;&#x7528;-secret"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F7F;&#x7528; Secret</h3>
<p>&#x524D;&#x9762;&#x5DF2;&#x7ECF;&#x4ECB;&#x7ECD;&#x8FC7; Ingress &#x4F7F;&#x7528; Secret&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x518D;&#x590D;&#x8FF0;&#x3002;</p>
<p>&#x5728; Pod &#x4E2D;&#x4F7F;&#x7528; Secret &#x6709;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x548C;&#x6587;&#x4EF6;&#x4E24;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;&#x5C06; Secret &#x6302;&#x8F7D;&#x5230; Pod &#x7684;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> secret1
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> secret1
    <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">volumeMounts</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> foo
      <span class="token key atrule">mountPath</span><span class="token punctuation">:</span> <span class="token string">&quot;/etc/foo&quot;</span>
      <span class="token key atrule">readOnly</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
  <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> foo
    <span class="token key atrule">secret</span><span class="token punctuation">:</span>
      <span class="token key atrule">secretName</span><span class="token punctuation">:</span> secret<span class="token punctuation">-</span>basic<span class="token punctuation">-</span>auth
</code></pre>
<blockquote>
<p>&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x521B;&#x5EFA;&#x7684;&#x57FA;&#x4E8E;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x7684; Secret&#xFF0C;&#x6709; username&#x3001;password &#x4E24;&#x4E2A;&#x5B57;&#x6BB5;&#x3002;</p>
</blockquote>
<p>&#x521B;&#x5EFA; Pod&#xFF0C;&#x7136;&#x540E;&#x67E5;&#x770B;&#x6620;&#x5C04;&#x5230; Pod &#x7684;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl exec secret1 -- ls /etc/foo</span>
password
username

root@master:~<span class="token comment"># kubectl exec secret1 -- cat /etc/foo/username</span>
adminroot
@master:~<span class="token comment"># kubectl exec secret1 -- cat /etc/foo/password</span>
t0p-Secret
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;Secret &#x4E2D;&#x6BCF;&#x4E00;&#x4E2A; &#x5B57;&#x6BB5;/Key&#xFF0C;&#x4F1A;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x540D;&#xFF0C;Value &#x662F;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4EE5;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x65B9;&#x5F0F;&#x4F7F;&#x7528; Secret&#xFF0C;&#x5176; YAML &#x8DDF; ConfigMap &#x7C7B;&#x4F3C;&#xFF0C;&#x6587;&#x4EF6;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> mysecret2
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> mysecret
    <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">env</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> SECRET_USERNAME
        <span class="token key atrule">valueFrom</span><span class="token punctuation">:</span>
          <span class="token key atrule">secretKeyRef</span><span class="token punctuation">:</span>
            <span class="token key atrule">name</span><span class="token punctuation">:</span> secret<span class="token punctuation">-</span>basic<span class="token punctuation">-</span>auth
            <span class="token key atrule">key</span><span class="token punctuation">:</span> username
      <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> SECRET_PASSWORD
        <span class="token key atrule">valueFrom</span><span class="token punctuation">:</span>
          <span class="token key atrule">secretKeyRef</span><span class="token punctuation">:</span>
            <span class="token key atrule">name</span><span class="token punctuation">:</span> secret<span class="token punctuation">-</span>basic<span class="token punctuation">-</span>auth
            <span class="token key atrule">key</span><span class="token punctuation">:</span> password
  <span class="token key atrule">restartPolicy</span><span class="token punctuation">:</span> Never
</code></pre>
<blockquote>
<p>&#x4F1A;&#x5C06; secret-basic-auth &#x4E2D;&#x7684; username &#x548C; password &#x4E24;&#x4E2A;&#x5B57;&#x6BB5;&#x6620;&#x5C04;&#x5230; Pod &#x7684; <code>SECRET_USERNAME</code> &#x3001;<code>SECRET_PASSWORD</code> &#x4E24;&#x4E2A;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x3002;</p>
</blockquote>
<p>&#x67E5;&#x770B; Pod &#x4E2D;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl exec mysecret2 -- env</span>
<span class="token assign-left variable">SECRET_USERNAME</span><span class="token operator">=</span>admin
<span class="token assign-left variable">SECRET_PASSWORD</span><span class="token operator">=</span>t0p-Secret
</code></pre>
<h3 id="&#x540C;&#x65F6;&#x4F7F;&#x7528;-configmap&#x3001;secret"><a name="&#x540C;&#x65F6;&#x4F7F;&#x7528;-configmap&#x3001;secret" class="anchor-navigation-ex-anchor" href="#&#x540C;&#x65F6;&#x4F7F;&#x7528;-configmap&#x3001;secret"><i class="fa fa-link" aria-hidden="true"></i></a>&#x540C;&#x65F6;&#x4F7F;&#x7528; ConfigMap&#x3001;Secret</h3>
<p>&#x5728;&#x96C6;&#x7FA4;&#x4E2D;&#x6709; db-config &#x548C; db-secret &#x4E24;&#x4E2A;&#x540D;&#x4E3A; ConfigMap &#x548C; Secret &#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x73B0;&#x5728;&#x9700;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; mydb &#x7684; Pod&#xFF0C;&#x5E76;&#x4E14;&#x4F7F;&#x7528; <code>mysql:5.7</code> &#x8FD9;&#x4E2A;&#x955C;&#x50CF;&#xFF0C;&#x8981;&#x6C42;&#x628A; db-config &#x548C; db-secret &#x4E2D;&#x7684;&#x6240;&#x6709;&#x952E;&#x503C;&#x5BF9;&#x4EE5;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x5F62;&#x5F0F;&#x6620;&#x5C04;&#x5230;&#x5BB9;&#x5668;&#x4E2D;&#x3002;</p>
<p>&#x5176; YAML &#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">creationTimestamp</span><span class="token punctuation">:</span> <span class="token null important">null</span>
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">run</span><span class="token punctuation">:</span> mydb
  <span class="token key atrule">name</span><span class="token punctuation">:</span> mydb
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">image</span><span class="token punctuation">:</span> mysql<span class="token punctuation">:</span><span class="token number">5.7</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> mydb
    <span class="token key atrule">envFrom</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">configMapRef</span><span class="token punctuation">:</span>
        <span class="token key atrule">name</span><span class="token punctuation">:</span> db<span class="token punctuation">-</span>config
    <span class="token punctuation">-</span> <span class="token key atrule">secretRef</span><span class="token punctuation">:</span>
        <span class="token key atrule">name</span><span class="token punctuation">:</span> db<span class="token punctuation">-</span>secret
    <span class="token key atrule">resources</span><span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  <span class="token key atrule">dnsPolicy</span><span class="token punctuation">:</span> ClusterFirst
  <span class="token key atrule">restartPolicy</span><span class="token punctuation">:</span> Always
<span class="token key atrule">status</span><span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; &#x75F4;&#x8005;&#x5DE5;&#x826F; 2021 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x6700;&#x540E;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2021-11-14 13:21:46
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1.volumes.html" class="navigation navigation-prev " aria-label="Previous page: 5.1 卷">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.nfts.html" class="navigation navigation-next " aria-label="Next page: 5.3 NFS卷">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"5.2 secret 和 ConfigMap 卷","level":"1.6.3","depth":2,"next":{"title":"5.3 NFS卷","level":"1.6.4","depth":2,"path":"5.volumes/3.nfts.md","ref":"5.volumes/3.nfts.md","articles":[]},"previous":{"title":"5.1 卷","level":"1.6.2","depth":2,"path":"5.volumes/1.volumes.md","ref":"5.volumes/1.volumes.md","articles":[]},"dir":"ltr"},"config":{"plugins":["tbfed-pagefooter","anchor-navigation-ex","mermaid-gb3","insert-logo","chapter-fold","advanced-emoji","github","splitter","alerts","popup","prism","-highlight","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy 痴者工良 2021","modify_label":"文档最后更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"prism":{"lang":{"flow":"typescript","shell":"bash"},"ignore":["mermaid","eval-js"],"css":["prismjs/themes/prism.css"],"js":["prismjs/prism.js","prismjs/components.js","prismjs/components/prism-go.min.js","prismjs/components/prism-yaml.min.js","prismjs/components/prism-bash.min.js","prismjs/components/prism-shell-session.min.js"]},"github":{"url":"https://github.com/whuanle/kubernetes"},"livereload":{},"splitter":{},"search":{},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":false},"alerts":{},"advanced-emoji":{"embedEmojis":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"insert-logo":{"style":"background: none; max-height: 50px; min-height: 50px","url":"/images/logo.jpg"}},"theme":"default","author":"痴者工良","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Kubernetes - 痴者工良","language":"zh-hans","links":{"sidebar":{"痴者工良的博客":"https://www.whuanle.cn"}},"gitbook":"*","description":"这是一本关于 Kubernetes 的书，作者 痴者工良"},"file":{"path":"5.volumes/2.secret_configmap.md","mtime":"2021-11-14T05:21:46.882Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-11-16T23:21:22.294Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

